﻿myApp.controller('DetailController', ['$scope', 'MuseumService', '$routeParams',
    function ($scope, MuseumService, $routeParams) {

        $scope.Greetings = "Hello Details.."
        $scope.Name = "foo";

        //Waether holder
        $scope.apiweather;
            
        // Museum details holder
        $scope.details;

        // New review form
        $scope.rv = {};
        $scope.rv.reviewer;
        $scope.rv.comment;
        $scope.rv.rating;
        $scope.index = $routeParams.id;

        // Get museum details
        $scope.DoGetMuseum = function () {
            MuseumService.getMuseum($scope.index).then(function (data) {
                $scope.details = data[0];
                // Get weather data by City (from Muesuem)
                $scope.DoGetWeather();
            })
        }

        // Get wather
        $scope.DoGetWeather = function () {
            MuseumService.getWeather($scope.details.City).then(function (data) {
                console.log(data);
                $scope.apiweather = data;
            })
        }


        // Reset function
        $scope.Reset = function () {
            $scope.rv.reviewer = "";
            $scope.rv.comment = "";
            $scope.rv.rating = "";
        }

        // Add a review
        $scope.DoAddReview = function () {
            // Build a new object and pass to service
            $scope.newRv = {
                Reviewer: $scope.rv.reviewer,
                Comment: $scope.rv.comment,
                Rating: $scope.rv.rating,
                MuseumId: $scope.index
            }
            console.log($scope.newRv);
            MuseumService.addReview($scope.newRv).then(function (data) {
                $scope.Reset();
                $scope.DoGetMuseum();
            })

        }
        // Call data when page loads
        $scope.DoGetMuseum();

        // Called from View...
        $scope.DoDeleteReview = function (did) {
            MuseumService.deleteReview(did).then(function (data) {
                $scope.DoGetMuseum();
                alert("it was deleted");

            })
        }


        // end
    }])